﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace VisionRoc_Measure
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            //Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            //AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(AppDomain_UnhandledException);
            // 禁止窗口多开
            Mutex mt = new Mutex(true, "VisionRoc_Measure", out bool IsCreateNew);
            if (!IsCreateNew)
            {
                MessageBox.Show("VisionRoc_Measure程序已打开");
                Application.Exit();
                return;
            }
            Application.Run(new FormMain());
        }

        private static void AppDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            string errorMessageIsTerminating = $"非UI线程异常:IsTerminating{e.IsTerminating.ToString()}";
            string errorMessageExceptionObject = $"非UI线程异常:ExceptionObject{e.ExceptionObject.ToString()}";
            Console.WriteLine(errorMessageIsTerminating);
            Console.WriteLine(errorMessageExceptionObject);
            
        }

        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            string errorMessageStackTrace = $"UI线程异常:StackTrace{e.Exception.StackTrace.ToString()}";
            string errorMessageExceptionMessage = $"UI线程异常:ExceptionObject{e.Exception.Message.ToString()}";
            Console.WriteLine(errorMessageStackTrace);
            Console.WriteLine(errorMessageExceptionMessage);
        }

    }
}
